package com.useeinfo.oa.modules.maintaining.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.maintaining.entity.FacilityMaintain;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/28 17:21
 * @Description:
 */
@Repository("facilityMaintainDao")
public class FacilityMaintainDao implements CrudDao<FacilityMaintain> {

    private final static Logger logger = LoggerFactory.getLogger(FacilityMaintainDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    public List<FacilityMaintain> findByFacilityId(Long facilityId,String  serial) {
        List<FacilityMaintain> facilityMaintainList = this.entityManager.createQuery("select fm from FacilityMaintain fm where fm.serial = :serial and fm.facilityId = :facilityId and fm.deleteStatus = false" , FacilityMaintain.class)
                .setParameter("serial", serial)
                .setParameter("facilityId",facilityId).getResultList();
        if (CollectionUtils.isEmpty(facilityMaintainList)) {
            return null;
        }
        return facilityMaintainList;
    }



    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String plannerId = queryHash.get("plannerId");
        if (!StringUtils.isEmpty(plannerId)) {
            conditionHash.put("(s.plannerOrder.id = ?{paramIndex}) ", StringConverters.null2Long(plannerId));
        }
        String serial = queryHash.get("serial");
        if (!StringUtils.isEmpty(serial)) {
            conditionHash.put("(s.serial = ?{paramIndex}) ", serial);
        }
        String beforeTimeStamp = queryHash.get("beforeTimeStamp");
        if (!StringUtils.isEmpty(beforeTimeStamp)) {
            conditionHash.put("(s.createDate < ?{paramIndex}) ", StringConverters.ToDateTime(beforeTimeStamp));
        }
        String noId = queryHash.get("noId");
        if (!StringUtils.isEmpty(noId)) {
            conditionHash.put("(s.plannerOrder.id is null or s.maintainId is null and 1=?{paramIndex}) ", 1);
        }
        String maintainId = queryHash.get("maintainId");
        if (!StringUtils.isEmpty(maintainId)) {
            conditionHash.put("s.maintainId is not null and 1=?{paramIndex}", 1);
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from FacilityMaintain s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<FacilityMaintain> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from FacilityMaintain s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<FacilityMaintain> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, FacilityMaintain.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public FacilityMaintain findModel(Long id) {
        return entityManager.find(FacilityMaintain.class, id);
    }

    @Override
    public Integer add(FacilityMaintain model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("FacilityMaintainDaoImpl添加facilityMaintain成功！");
        return 1;
    }

    @Override
    public Integer update(FacilityMaintain model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        FacilityMaintain facilityMaintain = entityManager.find(FacilityMaintain.class, id);
        entityManager.remove(facilityMaintain);
        return 1;
    }

    @Override
    public Integer deleteLogic(FacilityMaintain model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
